package qc.module.ehs.mapper;

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;
import qc.common.core.enums.ResourceStatusFlagEnum;
import qc.common.core.utils.StringSplitUtil;
import qc.module.ehs.dto.check.CheckTableDto;
import qc.module.ehs.dto.check.CheckTableFullDto;
import qc.module.ehs.entity.EhsCheckTable;

/**
 * EhsCheckTableMapper
 *
 * @author QuCheng Tech
 * @create 2024/10/23
 */
@Mapper
public interface EhsCheckTableMapper {
    EhsCheckTableMapper MAPPER = Mappers.getMapper(EhsCheckTableMapper.class);

    /**
     * entity -> dto
     * @param en
     * @return
     */
    @Mapping(target = "status", source = "status.index")
    @Mapping(target = "ucnm", ignore = true)
    @Mapping(target = "unitids", ignore = true)
    @Mapping(target = "unitnames", ignore = true)
    CheckTableDto toDto(EhsCheckTable en);

    /**
     * dto -> entity
     * @param dto
     * @return
     */
    @Mapping(target = "status", ignore = true)
    @Mapping(target = "unitids", ignore = true)
    EhsCheckTable toEntity(CheckTableDto dto);

    default EhsCheckTable dtoToEntity(CheckTableDto dto) {
        EhsCheckTable en = EhsCheckTableMapper.MAPPER.toEntity(dto);

        //单元ID集合处理
        if (dto.getUnitids() != null && dto.getUnitids().length > 0x0) {
            en.setUnitids(StringSplitUtil.Join(dto.getUnitids()));
        }

        //检查表状态枚举转换
        en.setStatus(getResourceStatusFlagEnum(dto.getStatus()));

        return en;
    }

    /**
     * entity -> CheckTableFullDto
     * @param en
     * @return
     */
    @Mapping(target = "status", source = "status.index")
    @Mapping(target = "ucnm", ignore = true)
    @Mapping(target = "unitids", ignore = true)
    @Mapping(target = "unitnames", ignore = true)
    CheckTableFullDto toCheckTableFullDto(EhsCheckTable en);

    static ResourceStatusFlagEnum getResourceStatusFlagEnum(Integer index) {
        if (index != null && index > 0x0) {
            for (ResourceStatusFlagEnum item : ResourceStatusFlagEnum.values()) {
                if (item.getIndex() == index) {//判断序号使用getIndex，判断内容使用getName
                    return item;
                }
            }
        }

        //默认状态为0--正常
        return ResourceStatusFlagEnum.NORMAL;
    }
}
